<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of tblCategories
 *
 * @author chema_000
 */
class Admin_Models_tblCategories extends Libs_Model{
    public function __construct()
    {
        parent::__construct();
        $this->queryUnit = new Libs_QueryUnit();
    }
    
    private $id;
    private $name;
    private $description;
    private $parent_id;
    
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getDescription() {
        return $this->description;
    }

    public function getParent_id() {
        return $this->parent_id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function setParent_id($parent_id) {
        $this->parent_id = $parent_id;
    }
    
    public function getAllCat() {
        $listCat = array();
        $execute = $this->queryUnit->getSelect('tbl_categories');
        if (mysql_num_rows($execute) > 0) {
            while ($row = mysql_fetch_assoc($execute)) {
                $listCat[] = $this->setCategoryValues($row);
            }
        }
        return $listCat;
    }
    
    public function getParentCat(){
        $id = 0;
        $listCat = array();
        $execute = $this->queryUnit->getCatByParentId('tbl_categories',$id);
        if (mysql_num_rows($execute) > 0) {
            while ($row = mysql_fetch_assoc($execute)) {
                $cat = $this->setCategoryValues($row);
                $listCat[] = $cat;
            }
        }
        return $listCat;
    }
    
    public function getCatByParentId($id){
        $listCat = array();
        $execute = $this->queryUnit->getCatByParentId('tbl_categories',$id);
        if (mysql_num_rows($execute) > 0) {
            while ($row = mysql_fetch_assoc($execute)) {
                $cat = $this->setCategoryValues($row);
                $listCat[] = $cat;
            }
        }
        return $listCat;
    }
    
    public function getCatByID($id){
        $cat = null;
            $execute = $this->queryUnit->getSelect("tbl_categories", "id='$id'");
            if (mysql_num_rows($execute) > 0) {
                while ($row = mysql_fetch_assoc($execute)) {
                    $cat = $this->setCategoryValues($row);
                }
            }
        return $cat;
    }
    
    public function setCategoryValues($row)
    {
        $cat = new Admin_Models_tblCategories();
        $cat->setId($row['id']);
        $cat->setName($row['name']);
        $cat->setDescription($row['description']);
        $cat->getParent_id($row['parent_id']);
        return $cat;
    }

    public function getColumnAndValue(Admin_Models_tblCategories $cat, $isKey = false)
    {
        $arr_data= array();
        if ($isKey) {
            $arr_data['id']=$cat->getId();
        }
        $arr_data['name']=$cat->getName();
        $arr_data['description']=$cat->getDescription();
        $arr_data['parent_id']=$cat->getParent_id();
        return $arr_data;
    }
    
    public function insertCat(Admin_Models_tblCategories $cat)
    {
        $execute=$this->queryUnit->getInsert('tbl_categories', $this->getColumnAndValue($cat));
        if($execute){
            return true;
        }
        return false;
    }

    public function updateCat(Admin_Models_tblCategories $cat, $id)
    {
        $execute=$this->queryUnit->getUpdate('tbl_categories', $this->getColumnAndValue($cat),"id='$id'");
        if($execute){
            return true;
        }
        return false;
    }

    public function deleteCategory($id){
        $execute= $this->queryUnit->getDelete('tbl_categories',"id='$id'");
        if($execute){
            return true;
        }
        return false;
    }
}
